package com.xingxing.config;

import com.xingxing.pojo.Configuration;
import com.xingxing.pojo.MapperdStatement;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import java.io.InputStream;
import java.util.List;

/**
 * @program: IPersistence_test
 * @description:
 * @author: xinghaozhao
 * @create: 2020-08-22
 */
public class XMLMapperBuilder {

    private Configuration configuration;

    public XMLMapperBuilder(Configuration configuration) {
        this.configuration = configuration;
    }

    public void parse(InputStream inputStream) throws DocumentException {

        Document document = new SAXReader().read(inputStream);

        Element rootElement = document.getRootElement();
        String namespace = rootElement.attributeValue("namespace");

        List<Element> list = rootElement.selectNodes("//select");

        list.forEach(element -> {
            MapperdStatement mapperdStatement = new MapperdStatement();
            String id = element.attributeValue("id");
            mapperdStatement.setId(id);
            mapperdStatement.setParamterType(element.attributeValue("paramterType"));
            mapperdStatement.setResultType(element.attributeValue("resultType"));
            mapperdStatement.setSql(element.getTextTrim());
            String key = namespace + "." + id;
            configuration.getMapperdStatementMap().put(key,mapperdStatement);

        });

    }
}
